package com.hotec.PROJECT_NAME.modules.cmd.config;

import com.hotec.PROJECT_NAME.modules.cmd.CommandExecutor;
import com.hotec.PROJECT_NAME.modules.cmd.CommandExecutorImpl;
import com.hotec.PROJECT_NAME.modules.cmd.interceptor.CommandInterceptor;
import com.hotec.PROJECT_NAME.modules.cmd.interceptor.CommandInvoker;
import com.hotec.PROJECT_NAME.modules.cmd.interceptor.LogInterceptor;
import com.hotec.PROJECT_NAME.modules.cmd.support.CommandContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ldz666 on 2022/9/14.
 */
@Configuration
public class CmdConfig {

    @Bean
    public CommandExecutor getCommandExecutor() {
        List<CommandInterceptor> interceptors = new ArrayList<CommandInterceptor>();
        interceptors.add(getLogIntegerceptor());
        interceptors.add(getCommandInvoker());

        for (int i = 0; i < interceptors.size() - 1; i++) {
            interceptors.get(i).setNext(interceptors.get(i + 1));
        }
        return new CommandExecutorImpl(interceptors.get(0), getCommandContext());
    }

    @Bean
    public CommandContext getCommandContext(){
        return new CommandContext();
    }

    @Bean
    public CommandInvoker getCommandInvoker(){

        return new CommandInvoker();
    }
    @Bean LogInterceptor getLogIntegerceptor(){

        return new LogInterceptor();
    }
}
